package listeners;

import org.antlr.v4.misc.OrderedHashMap;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

public class TestPropertyFileVisitor {
    public static class PropertyFileVisitor extends PropertyFileBaseVisitor<Void> {
        Map<String, String> props = new OrderedHashMap<>();
        public Void visitProp(PropertyFileParser.PropContext ctx) {
            String id = ctx.ID().getText();
            String value = ctx.STRING().getText();
            props.put(id, value);
            return null;
        }

        public static void main(String[] args) throws IOException {
            String inputFile = null;
            if (args.length > 0) {
                inputFile = args[0];
            }
            InputStream is = System.in;
            if (inputFile != null) {
                is = new FileInputStream(inputFile);
            }
            CharStream input = CharStreams.fromStream(is);
            PropertyFileLexer lexer = new PropertyFileLexer(input);
            CommonTokenStream tokenStream = new CommonTokenStream(lexer);
            PropertyFileParser parser = new PropertyFileParser(tokenStream);
            ParseTree tree = parser.file();
            PropertyFileVisitor fileVisitor = new PropertyFileVisitor();
            fileVisitor.visit(tree);
            System.out.println(fileVisitor.props);
        }

    }
}
